package app.kotlin_1.fragment

import android.content.DialogInterface
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v7.app.AlertDialog
import android.support.v7.widget.GridLayoutManager
import android.view.View
import app.kotlin_1.R
import app.kotlin_1.adapter.MyKindAdapter
import app.kotlin_1.base.BaseFragment
import app.kotlin_1.bean.KindBean
import app.kotlin_1.utils.AppUtils
import app.kotlin_1.utils.XPermissionUtils
import kotlinx.android.synthetic.main.fragment_my.view.*
import kotlinx.android.synthetic.main.title_common.view.*
import android.content.Intent
import android.net.Uri


/**
 * Created by 刘智强 on 2017/9/19.
 */
class MyFragment : BaseFragment() {
    private val kindDatas = mutableListOf<KindBean>()
    private val perrsions = arrayOf(android.Manifest.permission.READ_PHONE_STATE)

//    companion object {
//        fun newInstance(args: String): MyFragment {
//            var fragment = MyFragment()
//            var b = Bundle()
//            b.putString("args", args)
//            fragment.arguments = b
//            return fragment
//        }
//    }

    override fun getLayoutId(): Int = R.layout.fragment_my

    override fun initData() {
        rootView?.tvTitle?.text = "我的"

    }

    override fun initView(v: View) {
    }

    override fun initListener() {
        rootView?.sdvUserIcon?.setOnClickListener(this)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun setOnClick(v: View?) {
        when (v?.id) {
            R.id.sdvUserIcon -> XPermissionUtils.requestPermissions(context!!, XPermissionUtils.PHONE, perrsions, object : XPermissionUtils.OnPermissionListener {
                override fun showRationale(permissions: Array<String>) {
                    //被彻底禁止
                    AlertDialog.Builder(context!!).setTitle("提示")
                            .setMessage("${permissions[0]}被永久关闭，请点击OK去打开")
                            .setPositiveButton("OK") { dialog, which ->
                                run {
                                    dialog.dismiss()
                                    AppUtils.goAppSettingIntent(context)


                                }
                            }
                            .setNegativeButton("CANCLE") { dialog, which ->
                                run {
                                    dialog.dismiss()

                                }
                            }
                            .create().show()
                }

                override fun onPermissionGranted() {
                    //获取权限成功
                    AlertDialog.Builder(context!!).setTitle("提示")
                            .setMessage("获取照相机权限成功")
                            .setPositiveButton("OK") { dialog, which ->
                                run {
                                    val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "1111"))
                                    startActivity(intent)
                                    dialog.dismiss()
                                }
                            }.create().show()
                }

                override fun onPermissionDenied(deniedPermissions: Array<String>) {
                    //权限被禁止
                    AlertDialog.Builder(context!!).setTitle("提示")
                            .setMessage("获取照相机权限失败")
                            .setPositiveButton("OK") { dialog, which -> dialog.dismiss() }
                            .create().show()
                }


            })

        }
    }

    override fun requestData() {
    }

    override fun setData() {
        initKind()


    }

    private fun initKind() {
        kindDatas.add(KindBean(R.drawable.prize, "奖品", 0, false))
        kindDatas.add(KindBean(R.drawable.gift, "礼包", 0, true))
        kindDatas.add(KindBean(R.drawable.document, "评论", 0, false))
        kindDatas.add(KindBean(R.drawable.message, "消息", 12, false))
        rootView?.rvKind?.layoutManager = GridLayoutManager(context, 4)
        rootView?.rvKind?.adapter = MyKindAdapter(context, kindDatas, R.layout.item_my_kind)

    }


}